package de.fhtrier.mp.wollmaus.map;

import java.awt.Point;
import java.util.ArrayList;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.particles.ParticleSystem;
import org.newdawn.slick.tiled.TiledMap;

import de.fhtrier.mp.wollmaus.constants.Constants;
import de.fhtrier.mp.wollmaus.entity.Entity;
import de.fhtrier.mp.wollmaus.entity.Note;
import de.fhtrier.mp.wollmaus.gamestate.GameState;
import de.fhtrier.mp.wollmaus.util.AssetManager;

public class Stage {

	private ArrayList<Hindernis> hindernisse;
	private ArrayList<Point> teleports;
	private ArrayList<Note> notes;
	private ArrayList<ParticleSystem> particles;
	private ArrayList<Entity> entities;
	private TiledMap map;
	private Image img;
	private Point startPos;

	private StageArrow arrow;

	public Stage(String mapPath, String imgPath) {
		startPos = new Point(85, 85);
		this.hindernisse = new ArrayList<Hindernis>();
		teleports = new ArrayList<Point>();

		particles = new ArrayList<ParticleSystem>();
		notes = new ArrayList<Note>();
		try {
			this.map = new TiledMap(mapPath);
			this.img = new Image(imgPath);

			// Map für Helper-Objekte durchgehen
			for (int x = 0; x < map.getWidth(); ++x) {
				for (int y = 0; y < map.getHeight(); ++y) {
					for (int ebene=1;ebene<map.getLayerCount();++ebene) {
						int tileId = map.getTileId(x, y, ebene);
						// Kollisionen
						if (map.getTileProperty(tileId, "blocked", "0").equals("1")) {
							Hindernis h = new Hindernis(new Point(x * Constants.TILESIZE, y * Constants.TILESIZE), Constants.TILESIZE, Constants.TILESIZE);
							hindernisse.add(h);
						}
						// Startposition
						else if (map.getTileProperty(tileId, "start", "0").equals("1")) {
							startPos = new Point(x * Constants.TILESIZE, y * Constants.TILESIZE);
						}
						// Teleport
						else if (map.getTileProperty(tileId, "teleport", "0").equals("1")) {
							teleports.add(new Point(x * Constants.TILESIZE, y * Constants.TILESIZE));
						}
						// Noten
						else if (!map.getTileProperty(tileId, "note", "0").equals("0")) {
							Integer i = new Integer(map.getTileProperty(tileId, "note", "0"));
							notes.add(new Note(x * Constants.TILESIZE, y * Constants.TILESIZE + 30, i.intValue() - 1));
						}
						// Helper-Pfeil
						else if (map.getTileProperty(tileId, "arrow", "0").equals("1")) {
							arrow = new StageArrow(x * Constants.TILESIZE, y * Constants.TILESIZE);
						}
						// Partikel
						else if (!map.getTileProperty(tileId, "particle", "0").equals("0")) {
							Integer id = new Integer(map.getTileProperty(tileId, "particle", "0"));
							ParticleSystem p = AssetManager.getInstance().getParticle(AssetManager.PART_WOOL+id-1).duplicate();
							p.setPosition(x * Constants.TILESIZE, y * Constants.TILESIZE + 0.5f * Constants.TILESIZE);
							particles.add(p);
						}
					}
				}
			}

			
		}
		catch (SlickException e) {
			e.printStackTrace();
		}
		entities = new ArrayList<Entity>();
		
		if (imgPath.equals("Files/maps/Screen4.jpg")) {
			this.addEntity(new Entity(new Vector2f(86,0),AssetManager.ANI_SPIDER));
		}
	}
	
	public void update(GameContainer gc, int delta) {
		for (ParticleSystem p : particles) {
			p.update(delta);
		}
		for (Entity e : entities) {
			e.update(gc, delta);
		}
	}

	public void render(GameContainer gc, Graphics g) {
		this.getImage().draw();

		for (Note e : notes) {
			e.render(gc, g);
		}
		if (arrow != null && isLevelSolved()) {
			arrow.render(gc, g);
		}
		
		for (ParticleSystem p : particles) {
			p.render();
		}
		
		for (Entity e : entities) {
			e.render(g, gc);
		}
	}

	public boolean isLevelSolved() {
		return (GameState.getInstance().getPlayer().getCollectedNotes() >= notes.size());
	}

	public Image getImage() {
		return img;
	}

	public TiledMap getMap() {
		return map;
	}

	public ArrayList<Hindernis> getHindernisse() {
		return hindernisse;
	}

	public ArrayList<Point> getTeleports() {
		return teleports;
	}

	public Point getStartPos() {
		return startPos;
	}

	public ArrayList<Note> getNotes() {
		return notes;
	}
	
	public void addEntity(Entity e) {
		entities.add(e);
	}
	
	public void removeEntity(Entity e) {
		entities.remove(e);
	}
}
